#include "rotation.h"
#include "ui_rotation.h"
#include "ventana.h"
#include "ventana.h"

rotation::rotation(QWidget *parent) :
    QWidget(parent),
    m_ui(new Ui::rotation)
{
    m_ui->setupUi(this);
  //  check=false;
    inv=false;

}

rotation::~rotation()
{
    delete m_ui;
}

void rotation::changeEvent(QEvent *e)
{
    QWidget::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void rotation::on_pushButton_clicked()
{
    QImage p;
    inv=false;
    if(m_ui->Grad0->isChecked())
    {
        cos=1;
        sen=0;
        p=QImage(img->width(),img->height(),QImage::Format_Indexed8);
        inv=true;
    }
    else if(m_ui->Grad90->isChecked())
    {
        cos=0;
        sen=1;
        p=QImage(img->height(),img->width(),QImage::Format_Indexed8);
    }
    else if(m_ui->Grad180->isChecked())
    {
        cos=-1;
        sen=0;
        p=QImage(img->width(),img->height(),QImage::Format_Indexed8);
        inv=true;
    }
    else if(m_ui->Grad270->isChecked())
    {
        cos=0;
        sen=-1;
        p=QImage(img->height(),img->width(),QImage::Format_Indexed8);
    }
    p.setColorTable(img->colorTable());
    int x,y;
    int w=p.width(),h=p.height(),ox=img->width()/2,oy=img->height()/2;
    for(int i=0;i<w;++i){
        for(int j=0;j<h;++j){
            if(inv){
                x=(cos*(i-ox)-sen*(j-oy)) + ox;
                y=(sen*(i-ox)+cos*(j-oy)) + oy;
            }else{
                x=(cos*(i-oy)-sen*(j-ox)) + ox;
                y=(sen*(i-oy)+cos*(j-ox)) + oy;
            }
            if(x>=0 && x<img->width() && y>=0 && y<img->height())
                p.setPixel(i,j,img->pixelIndex(x,y));
        }
    }
    *img=p;

     ((Ventana*)v)->getHistogram();
    //check=true;

    this->hide();
}

